/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Pagos.Dialogos.Html;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author FCCCRTI05
 */
public class Companias extends HttpServlet
{

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        //REINICIO DEL BUFFER
        response.reset();
        response.resetBuffer();
        response.setContentType("text/html; charset=ISO-8859-1");
        response.setHeader("Cache-Control","no-cache");
        try
        {
            //CONEXION A LA BD
            db.Conexion conectar = new db.Conexion();
            conectar.ConectarMySQL();
            Connection conex = conectar.ObtenerConexionMySQL();
            if (conectar != null)
            {
                if (conex.isValid(0) == true)
                {
                    if ((request.getParameter("flag") != null)
                    &&(request.getParameter("flag").equals("ok")))
                    {
                        Statement s = null;
                        ResultSet rs = null;
                        String consulta = null,pais = null,companias = null;
                        if (request.getParameter("pais") != null)
                        {
                            pais = new db.Utilidades().CodigoPais(conex,request.getParameter("pais"));
                            consulta = "select id_cia,pais_cia,descrip_cia from companias"+
                            " where pais_cia like "+"'"+pais+"'"+" order by descrip_cia";
                        }
                        else
                        {
                            //CONEXION A LA BD SQL
                            db.Conexion conectar_sql = (db.Conexion)request.getSession().getAttribute("db");
                            Connection conex_sql = conectar_sql.ObtenerConexionSQLServer();
                            if (conectar_sql.AccesoTodasCompanias() == true)
                            {
                                consulta = "select id_cia,pais_cia,descrip_cia from companias"+
                                " order by descrip_cia";
                            }
                            else
                            if (conectar_sql.CompaniasMultiplesUsuario() == true)
                            {
                                companias = conectar_sql.ObtenerCompaniasUsuario();
                                companias = companias.substring(0,companias.length()-1);
                                consulta = "select id_cia,pais_cia,descrip_cia from companias"+
                                " where descrip_cia in ("+companias+") order by descrip_cia";
                            }
                        }
                        String codigo = null,descripcion = null;
                        //SALIDA HTML
                        out.write("<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"2\" class=\"bordes\">"+"\n");
                        out.write("<tr style=\"background-color:#99ffcc; font-weight:bold;\" class=\"bordes\">"+"\n");
                        out.write("<td align=\"center\" class=\"bordes\"><b>Clic sobre descripci&oacute;n de la \"Empresa\" para seleccionarla.</b></td>"+"\n");
                        out.write("</tr>"+"\n");
                        out.write("<tr style=\"background-color:#0FF; font-weight:bold;\" class=\"bordes\">"+"\n");
                        out.write("<td align=\"left\" valign=\"top\" class=\"bordes\"><b>Descripci&oacute;n</b></td>"+"\n");
                        out.write("</tr>"+"\n");
                        s = conex.createStatement();
                        rs = s.executeQuery(consulta);
                        while(rs.next())
                        {
                            codigo = rs.getString("id_cia");
                            descripcion = rs.getString("descrip_cia");
                            out.write("<tr class=\"bordes\">"+"\n");
                            out.write("<td align=\"left\" valign=\"top\" class=\"bordes\"><b><a href=\"javascript:void(0);\" class=\"menu_top\" onclick=\"SeleccionarCompania('"+descripcion+"','"+codigo+"');\">"+descripcion+"</a></b></td>"+"\n");
                            out.write("</tr>"+"\n");
                        }
                        out.write("</table>"+"\n");
                        rs.close();
                        s.close();
                    }
                }
                if (conex.isValid(0) == false)
                    out.write("<p align=\"center\">"+
                    "<img src=\"/jde/img/alerta.png\" width=\"24\" height=\"24\" border=\"0\" align=\"absmiddle\" />"+
                    "&nbsp;<b><a href=\"/jde/Pagos/?sesion_finalizada=ok\" class=\"menu_top\">La sesi&oacute;n ha finalizado,"+
                    " haga clic en este texto para reinicar.</a></b></p>");
            }
            if (conectar == null)
                out.write("<p align=\"center\">"+
                "<img src=\"/jde/img/alerta.png\" width=\"24\" height=\"24\" border=\"0\" align=\"absmiddle\" />"+
                "&nbsp;<b><a href=\"/jde/Pagos/?sesion_finalizada=ok\" class=\"menu_top\">La sesi&oacute;n ha finalizado,"+
                " haga clic en este texto para reinicar.</a></b></p>");
            conex.close();
        }
        catch (Exception x)
        {
            out.write("<p align=\"center\">"+
            "<img src=\"/jde/img/alerta.png\" width=\"24\" height=\"24\" border=\"0\" align=\"absmiddle\" />"+
            "&nbsp;<b>Se produjo un error: "+new db.Utilidades().ObtenerStringStackTrace(x)+".</b></p>");
        }
        finally
        {
            out.flush();
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }
}